package com.ccp.dev.form.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.form.model.FormTemplate;
import com.ccp.dev.form.service.FormTemplateService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * BPM_FORM_TEMPLATE 控制器类
 *
 * @author zhaowj
 */
@Controller
@RequestMapping(UrlConstants.PROJECT_ROOT + UrlConstants.PROJECT_MODULE_FORM + "/formTemplate")
@Action(ownermodel = SysAuditModelType.FORM_MANAGEMENT)
public class FormTemplateController extends SysBaseController {
    @Resource
    private FormTemplateService formTemplateService;

    /**
     * 取得表单模板列表
     *
     * @param request 请求
     * @return ResultData
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @ResponseBody
    @Action(description = "取得表单模板列表", exectype = "查询日志",
            detail = "取得表单模板列表<#if success>成功<#else>失败</#if>")
    public ResultData list(HttpServletRequest request) {
        QueryFilter filter = new QueryFilter(request);
        List<FormTemplate> list;
        try {
            list = formTemplateService.queryList(filter);
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("query.failure");
        }
        return getResultDataSuccess("query.success", list.size(), list);
    }

    /**
     * 取得初始化模板信息
     * @return ResultData
     */
    @RequestMapping("/init")
    @ResponseBody
    @Action(description = "初始化业务数据模板", exectype = "操作日志",
            detail = "初始化业务数据模板<#if success>失败<#else>成功</#if>")
    public ResultData init() {
        try {
            formTemplateService.initAllTemplate();
            SysAuditThreadLocalHolder.putParamerter("success", true);
        } catch (Exception ex) {
            ex.printStackTrace();
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return getResultDataError("operation.failure");
        }
        return getResultDataSuccess("operation.success");
    }
}
